package site.xuxingcan.wifihost;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by xuxingcan on 2016/10/25.
 */
public class DialogHelper {
    public interface DialogCallBack {
        void onConfirm(DialogInterface dialog, String s);

        void onCancel(DialogInterface dialog);
    }

    public static void showEditTextDialog(Context mContext, String title, String msg, final DialogCallBack callBack) {
        try {
            LinearLayout mLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.edit_text_dialog, null);
            final EditText mEditText = (EditText) mLayout.findViewById(R.id.edit_text);
            TextView mTextView = (TextView) mLayout.findViewById(R.id.text);
            if (msg == null) {
                mTextView.setVisibility(View.GONE);
            } else {
                mTextView.setText(msg);
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);  //先得到构造器
            builder.setTitle(title); //设置标题
            builder.setView(mLayout);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (callBack == null) {
                        dialog.dismiss();
                    } else {
                        callBack.onConfirm(dialog, mEditText.getText().toString());
                    }
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (callBack != null) {
                        callBack.onCancel(dialog);
                    }
                }
            });
            builder.setCancelable(false);
            builder.create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static AlertDialog showOnlyMessageDialog(Context mContext, String title, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);  //先得到构造器
        builder.setTitle(title); //设置标题
        builder.setMessage(msg); //设置内容
        builder.setCancelable(false);
        AlertDialog dialog = builder.create();
        dialog.show();
        return dialog;
    }

    public static void showMessageDialog(Context mContext, String title, String msg, final DialogCallBack callBack) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);  //先得到构造器
        builder.setTitle(title); //设置标题
        builder.setMessage(msg); //设置内容
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (callBack == null) {
                    dialog.dismiss();
                } else {
                    callBack.onConfirm(dialog, null);
                }
            }
        });
        builder.setCancelable(false);
        builder.create().show();
    }

    public interface MenuDialogCallBack {
        void onItemOnClick(DialogInterface dialog, int which);
    }

    public static void showMenuDialog(Context mContext, String title, String items[], final MenuDialogCallBack callBack) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(title);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (callBack == null) {
                    dialog.dismiss();
                } else {
                    callBack.onItemOnClick(dialog, which);
                }
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
